package com.hanthink.system.service.impl.manhour;

import java.util.List;

import com.hanthink.common.exception.CustomException;
import com.hanthink.common.utils.StringUtils;
import com.hanthink.system.domain.manhour.ProductionPlanInShift;
import com.hanthink.system.service.impl.SysUserServiceImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.hanthink.system.mapper.manhour.ProductionPlanAssistInShiftMapper;
import com.hanthink.system.domain.manhour.ProductionPlanAssistInShift;
import com.hanthink.system.service.manhour.IProductionPlanAssistInShiftService;

/**
 * 辅助用工上报Service业务层处理
 * 
 * @author hanthink
 * @date 2021-12-09
 */
@Service
public class ProductionPlanAssistInShiftServiceImpl implements IProductionPlanAssistInShiftService 
{
    private static final Logger log = LoggerFactory.getLogger(SysUserServiceImpl.class);


    @Autowired
    private ProductionPlanAssistInShiftMapper productionPlanAssistInShiftMapper;

    /**
     * 查询辅助用工上报
     * 
     * @param id 辅助用工上报ID
     * @return 辅助用工上报
     */
    @Override
    public ProductionPlanAssistInShift selectProductionPlanAssistInShiftById(Long id)
    {
        return productionPlanAssistInShiftMapper.selectProductionPlanAssistInShiftById(id);
    }

    /**
     * 查询辅助用工上报列表
     * 
     * @param productionPlanAssistInShift 辅助用工上报
     * @return 辅助用工上报
     */
    @Override
    public List<ProductionPlanAssistInShift> selectProductionPlanAssistInShiftList(ProductionPlanAssistInShift productionPlanAssistInShift)
    {
        return productionPlanAssistInShiftMapper.selectProductionPlanAssistInShiftList(productionPlanAssistInShift);
    }

    /**
     * 新增辅助用工上报
     * 
     * @param productionPlanAssistInShift 辅助用工上报
     * @return 结果
     */
    @Override
    public int insertProductionPlanAssistInShift(ProductionPlanAssistInShift productionPlanAssistInShift)
    {
        return productionPlanAssistInShiftMapper.insertProductionPlanAssistInShift(productionPlanAssistInShift);
    }

    /**
     * 修改辅助用工上报
     * 
     * @param productionPlanAssistInShift 辅助用工上报
     * @return 结果
     */
    @Override
    public int updateProductionPlanAssistInShift(ProductionPlanAssistInShift productionPlanAssistInShift)
    {
        return productionPlanAssistInShiftMapper.updateProductionPlanAssistInShift(productionPlanAssistInShift);
    }

    /**
     * 批量删除辅助用工上报
     * 
     * @param ids 需要删除的辅助用工上报ID
     * @return 结果
     */
    @Override
    public int deleteProductionPlanAssistInShiftByIds(Long[] ids)
    {
        return productionPlanAssistInShiftMapper.deleteProductionPlanAssistInShiftByIds(ids);
    }

    /**
     * 删除辅助用工上报信息
     * 
     * @param id 辅助用工上报ID
     * @return 结果
     */
    @Override
    public int deleteProductionPlanAssistInShiftById(Long id)
    {
        return productionPlanAssistInShiftMapper.deleteProductionPlanAssistInShiftById(id);
    }

    /**
     * 导入数据
     *
     * @param PrdPlanItemList     数据列表
     * @param isUpdateSupport 是否更新支持，如果已存在，则进行更新数据
     * @param operName        操作用户
     * @return 结果
     */
    @Override
    public String importPlanitem(List<ProductionPlanAssistInShift> PrdPlanItemList, Boolean isUpdateSupport, String operName) {
        if (StringUtils.isNull(PrdPlanItemList) || PrdPlanItemList.size() == 0) {
            throw new CustomException("导入的实际用工数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        int currentRowNum = 0;
        for (ProductionPlanAssistInShift prdPlanItem : PrdPlanItemList) {
            currentRowNum++;
            try {
                ProductionPlanAssistInShift items = productionPlanAssistInShiftMapper.selectProductionPlanAssistInShiftByKey(prdPlanItem);//.getPlanworkdatetime(),prdPlanItem.getPshiftid(),prdPlanItem.getMachineid() ,prdPlanItem.getMaterialid()) ;
                if (StringUtils.isNull(items)) {
                    prdPlanItem.setCreateBy(operName);
                    this.insertProductionPlanAssistInShift(prdPlanItem);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、辅助用工数据" + prdPlanItem.getPlanworkdatetime() + prdPlanItem.getPshiftid() + " 导入成功");
                } else if (isUpdateSupport) {
                    prdPlanItem.setUpdateBy(operName);
                    // prdPlanItem.setId(items.getId());
                    this.updateProductionPlanAssistInShift(prdPlanItem);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、辅助用工数据 " + prdPlanItem.getPlanworkdatetime() + prdPlanItem.getPshiftid()  + " 更新成功");
                    continue;
                } else {
                    failureNum++;
                    failureMsg.append("<br/>" + failureNum + "、辅助用工数据 " + prdPlanItem.getPlanworkdatetime() +  prdPlanItem.getPshiftid() + " 已存在");
                    continue;
                }
            }catch (Exception e) {
                failureNum++;
                String msg = "<br/>" + failureNum + "、辅助用工数据" + prdPlanItem.getCompanyid() + " 导入失败：";
                failureMsg.append(msg + e.getMessage());
                log.error(msg, e);
            }
        }
        if (failureNum > 0) {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new CustomException(failureMsg.toString());
        } else {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }
        return successMsg.toString();
    }

}
